package com.waypolice.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

/**
 * 将dateTime类型的日期转换成年月日时分的形式
 * @author wr
 *
 */
public class DateTimeUtil {
	
	/**
	 * Long类型的时间戳转换成date类型的时间
	 * @param time
	 * @return
	 */
	public static Date timeMiao(Long time){
	        Date date = new Date(time);  
		return date;
	}

	/**
	 * 获取当前时间毫秒数
	 * @return
	 */
	public static String getNowMilliseconds(){
		Date currentTime = new Date();
		long time = currentTime.getTime();
		String s = String.valueOf(time);
		return s;
	}
	
	/**
	 * 将毫秒数转换成String类型的yyyy-MM-dd HH-mm-ss时间格式
	 * @param sstime
	 * @return
	 * @throws ParseException 
	 */
	public static Date ssTimeTogetYear(Long sstime) throws ParseException{
		   Date date2 = new Date();  
		   date2.setTime(sstime);  
		 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 String format2 = format.format(date2);
		 Date date=format.parse(format2);
		return date;
	}

	/**
	 * 将毫秒数转换成String类型的yyyy-MM-dd HH-mm-ss时间格式
	 * @param sstime
	 * @return
	 * @throws ParseException
	 */
	public static Date ssTimeByShortDate(Long sstime) throws ParseException{
		Date date2 = new Date();
		date2.setTime(sstime);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String format2 = format.format(date2);
		Date date=format.parse(format2);
		return date;
	}

	/**
	 * 获取当前年份(String类型)
	 * @return
	 */
	public static String getYear(){
		  Calendar a=Calendar.getInstance();
		  int strYear = a.get(Calendar.YEAR);
		  String year = String.valueOf(strYear);
		return year;
	}
	
	/**
     * 获取两个日期之间的所有日期（yyyy-MM-dd）
     * 注释部分：这里是获取两个日期之间的所有日期（不包涵begin和end）
     * @Description TODO
     * @param begin
     * @param end
     * @return
     */
    public static List<Date> getBetweenDates(Date begin, Date end) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            List<Date> result = new ArrayList<Date>();
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(begin);
            /* Calendar tempEnd = Calendar.getInstance();
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
            tempEnd.setTime(end);
            while (tempStart.before(tempEnd)) {
                result.add(tempStart.getTime());
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }*/
         while(begin.getTime()<=end.getTime()){
             result.add(tempStart.getTime());
             tempStart.add(Calendar.DAY_OF_YEAR, 1);
             begin = tempStart.getTime();
         }
            return result;
    }

	/**
	 * yyyy-MM-dd HH:mm:ss转换成yyyy年MM月dd日 HH时mm分ss秒
	 * @param date
	 * @return
	 */
	public static String encodeDownloadFilename(Date date){
		DateFormat  df  = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
		return df.format(date);
	}
	
	/**
	 * yyyy-MM-dd HH:mm:ss转换成yyyy年MM月dd日 HH时mm分
	 * @param date
	 * @return
	 */
	public static String encodeMin(Date date){
		DateFormat  df  = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
		return df.format(date);
	}
	
	/**
	 * yyyy-MM-dd转换成yyyy年MM月dd日
	 * @param date
	 * @return
	 */
	public static String encodeDay(Date date){
		DateFormat  df  = new SimpleDateFormat("yyyy年MM月dd日");
		return df.format(date);
	}
	
	/**
	 * 获取当前时间月（String）
	 * @return
	 */
	public static String systemTime(){
		Date day=new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
		return df.format(day);
	}
	
	/**
	 * 获取当前时间（Date）
	 * @return
	 */
	public static String systemTimeDate(){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = df.format(new Date());
		
		return format;
	}
	
	
	/** 
	 * 获取时间(day为0时为当前时间)
	 *  
	 * @return返回短时间格式 yyyy-MM-dd 
	 */  
	public static Date getSqlDate(int day) {
		//System.currentTimeMillis()
		Date sqlDate = new java.sql.Date(System.currentTimeMillis());  
		return sqlDate;  
	}  
	  
	/** 
	 * 获取现在时间 (此方法获取当前时间会抛出异常,建议使用getNow()方法)
	 * @throws ParseException 
	 *  
	 * @return返回长时间格式 yyyy-MM-dd HH:mm:ss 
	 */  
	public static Date getNowDate() throws ParseException {  
	    Date currentTime = new Date();  
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	    String dateString = formatter.format(currentTime);  
	    
	    Date parse = formatter.parse(dateString);
	    
	  //  ParsePosition pos = new ParsePosition(19);  
	   // Date currentTime_2 = formatter.parse(dateString, pos);  
	    return parse;  
	}  
	  
	/** 
	 * 获取现在时间 
	 *  
	 * @return返回短时间格式 yyyy-MM-dd 
	 */  
	public static Date getNowDateShort() {  
	    Date currentTime = new Date();  
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
	    String dateString = formatter.format(currentTime); 
	    Date dat = DateTimeUtil.strToDate(dateString);
	    return dat;  
	}  
	  
	/** 
	 * 获取现在时间 
	 *  
	 * @return返回字符串格式 yyyy-MM-dd HH:mm:ss 
	 */  
	public static String getStringDate() {  
	    Date currentTime = new Date();  
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	    String dateString = formatter.format(currentTime);  
	    return dateString;  
	}  
	  
	/** 
	 * 获取现在时间 
	 *  
	 * @return 返回短时间字符串格式yyyy-MM-dd 
	 */  
	public static String getStringDateShort() {  
	    Date currentTime = new Date();  
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
	    String dateString = formatter.format(currentTime);  
	    return dateString;  
	}  
	  
	/** 
	 * 获取时间 小时:分;秒 HH:mm:ss 
	 *  
	 * @return 
	 */  
	public static String getTimeShort() {  
	    SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");  
	    Date currentTime = new Date();  
	    String dateString = formatter.format(currentTime);  
	    return dateString;  
	}  
	  
	/** 
	 * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss 
	 *  
	 * @param strDate 
	 * @return 
	 */  
	public static Date strToDateLong(String strDate) {  
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	    ParsePosition pos = new ParsePosition(0);  
	    Date strtodate = formatter.parse(strDate, pos);  
	    return strtodate;  
	}  
	  
	/** 
	 * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss 
	 *  
	 * @param dateDate 
	 * @return 
	 */  
	public static String dateToStrLong(java.util.Date dateDate) {  
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	    String dateString = formatter.format(dateDate);  
	    return dateString;  
	}  
	  
	/** 
	 * 将短时间格式时间转换为字符串 yyyy-MM-dd 
	 *  
	 * @param dateDate 
	 * @param
	 * @return 
	 */  
	public static String dateToStr(java.util.Date dateDate) {  
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
	    String dateString = formatter.format(dateDate);  
	    return dateString;  
	}  
	  
	/** 
	 * 将短时间格式字符串转换为时间 yyyy-MM-dd 
	 *  
	 * @param strDate 
	 * @return 
	 */  
	public static Date strToDate(String strDate) {  
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
	    ParsePosition pos = new ParsePosition(0);  
	    Date strtodate = formatter.parse(strDate, pos);  
	    return strtodate;  
	}



	/** 
	 * 得到现在时间 
	 *  
	 * @return 
	 */  
	public static Date getNow() {  
	    Date currentTime = new Date();  
	    return currentTime;  
	}  
	  
	/** 
	 * 提取一个月中的最后一天 
	 *  
	 * @param day 
	 * @return 
	 */  
	public static Date getLastDate(long day) {  
	    Date date = new Date();  
	    long date_3_hm = date.getTime() - 3600000 * 34 * day;  
	    Date date_3_hm_date = new Date(date_3_hm);  
	    return date_3_hm_date;  
	}  
	  
	/** 
	 * 得到现在时间 
	 *  
	 * @return 字符串 yyyyMMdd HHmmss 
	 */  
	public static String getStringToday() {  
	    Date currentTime = new Date();  
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");  
	    String dateString = formatter.format(currentTime);  
	    return dateString;  
	}  
	  
	/** 
	 * 得到现在小时 
	 */  
	public static String getHour() {  
	    Date currentTime = new Date();  
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	    String dateString = formatter.format(currentTime);  
	    String hour;  
	    hour = dateString.substring(11, 13);  
	    return hour;  
	}  
	  
	/** 
	 * 得到现在分钟 
	 *  
	 * @return 
	 */  
	public static String getTime() {  
	    Date currentTime = new Date();  
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	    String dateString = formatter.format(currentTime);  
	    String min;  
	    min = dateString.substring(14, 16);  
	    return min;  
	}  
	  
	/** 
	 * 根据用户传入的时间表示格式，返回当前时间的格式 如果是yyyyMMdd，注意字母y不能大写。 
	 *  
	 * @param sformat 
	 *            yyyyMMddhhmmss 
	 * @return 
	 */  
	public static String getUserDate(String sformat) {  
	    Date currentTime = new Date();  
	    SimpleDateFormat formatter = new SimpleDateFormat(sformat);  
	    String dateString = formatter.format(currentTime);  
	    return dateString;  
	}  
	  
	/** 
	 * 二个小时时间间的差值,必须保证二个时间都是"HH:MM"的格式，返回字符型的分钟 
	 */  
	public static String getTwoHour(String st1, String st2) {  
	    String[] kk = null;  
	    String[] jj = null;  
	    kk = st1.split(":");  
	    jj = st2.split(":");  
	    if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0])){
	    	return "0";  
	    } else {  
	        double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1])  
	                / 60;  
	        double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1])  
	                / 60;  
	        if ((y - u) > 0) {
	        	return y - u + "";  
	        }else {
	        	return "0";  
	        }
	    }  
	}  
	  
	/** 
	 * 得到二个日期间的间隔天数 
	 */  
	public static String getTwoDay(String sj1, String sj2) {  
	    SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");  
	    long day = 0;  
	    try {  
	        java.util.Date date = myFormatter.parse(sj1);  
	        java.util.Date mydate = myFormatter.parse(sj2);  
	        day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);  
	    } catch (Exception e) {  
	        return "";  
	    }  
	    return day + "";  
	}  
	  
	/** 
	 * 时间前推或后推分钟,其中JJ表示分钟. 
	 */  
	public static String getPreTime(String sj1, String jj) {  
	    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	    String mydate1 = "";  
	    try {  
	        Date date1 = format.parse(sj1);  
	        long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;  
	        date1.setTime(Time * 1000);  
	        mydate1 = format.format(date1);  
	    } catch (Exception e) {  
	    }  
	    return mydate1;  
	}  
	  
	/** 
	 * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数 
	 */  
	public static String getNextDay(String nowdate, String delay) {  
	    try {  
	        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
	        String mdate = "";  
	        Date d = strToDate(nowdate);  
	        long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24  
	                * 60 * 60;  
	        d.setTime(myTime * 1000);  
	        mdate = format.format(d);  
	        return mdate;  
	    } catch (Exception e) {  
	        return "";  
	    }  
	}  
	  
	/** 
	 * 判断是否润年 
	 *  
	 * @param ddate 
	 * @return 
	 */  
	public static boolean isLeapYear(String ddate) {  
	    /** 
	     * 详细设计： 1.被400整除是闰年，否则： 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年 
	     * 3.能被4整除同时能被100整除则不是闰年 
	     */  
	    Date d = strToDate(ddate);  
	    GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();  
	    gc.setTime(d);  
	    int year = gc.get(Calendar.YEAR);  
	    if ((year % 400) == 0){
	    	return true;  
	    } else if ((year % 4) == 0) {  
	        if ((year % 100) == 0){
	        	return false;  
	        }else {
	        	return true;  
	        } 
	    } else {
	    	return false;  
	    } 
	}  
	  
	/** 
	 * 返回美国时间格式 26 Apr 2006 
	 *  
	 * @param str 
	 * @return 
	 */  
	public static String getEDate(String str) {  
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
	    ParsePosition pos = new ParsePosition(0);  
	    Date strtodate = formatter.parse(str, pos);  
	    String j = strtodate.toString();  
	    String[] k = j.split(" ");  
	    return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);  
	}  
	  
	/** 
	 * 获取一个月的最后一天 
	 *  
	 * @param dat 
	 * @return 
	 */  
	public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd  
	    String str = dat.substring(0, 8);  
	    String month = dat.substring(5, 7);  
	    int mon = Integer.parseInt(month);  
	    if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8  
	            || mon == 10 || mon == 12) {  
	        str += "31";  
	    } else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {  
	        str += "30";  
	    } else {  
	        if (isLeapYear(dat)) {  
	            str += "29";  
	        } else {  
	            str += "28";  
	        }  
	    }  
	    return str;  
	}  
	  
	/** 
	 * 判断二个时间是否在同一个周 
	 *  
	 * @param date1 
	 * @param date2 
	 * @return 
	 */  
	public static boolean isSameWeekDates(Date date1, Date date2) {  
	    Calendar cal1 = Calendar.getInstance();  
	    Calendar cal2 = Calendar.getInstance();  
	    cal1.setTime(date1);  
	    cal2.setTime(date2);  
	    int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);  
	    if (0 == subYear) {  
	        if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2  
	                .get(Calendar.WEEK_OF_YEAR)){
	        	return true;  
	        }  
	    } else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {  
	        // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周  
	        if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2  
	                .get(Calendar.WEEK_OF_YEAR)){
	        	return true;  
	        }  
	    } else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {  
	        if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2  
	                .get(Calendar.WEEK_OF_YEAR)) {
	        	return true;  
	        } 
	    }  
	    return false;  
	}  
	  
	/** 
	 * 产生周序列,即得到当前时间所在的年度是第几周 
	 *  
	 * @return 
	 */  
	public static String getSeqWeek() {  
	    Calendar c = Calendar.getInstance(Locale.CHINA);  
	    String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));  
	    if (week.length() == 1){
	    	week = "0" + week;  
	    }  
	    String year = Integer.toString(c.get(Calendar.YEAR));  
	    return year + week;  
	}  
	  
	/** 
	 * 获得一个日期所在的周的星期几的日期 
	 *  
	 * @param sdate 长类型yyyy-MM-dd HH:mm:ss
	 * @param num 星期一:1,.....星期六:6,星期天:0
	 * @return  短类型yyyy-MM-dd 如:2017-12-11
	 */  
	public static String getWeek(String sdate, String num) {  
	    // 再转换为时间  
	    Date dd = strToDate(sdate);  
	    Calendar c = Calendar.getInstance();  
	    c.setTime(dd);  
	    switch (num) {
		case "1":
			// 返回星期一所在的日期 
			c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 
			break;
		case "2":
			// 返回星期二所在的日期 
			c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY); 
			break;
		case "3":
			// 返回星期三所在的日期 
			c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);  
			break;
		case "4":
			// 返回星期四所在的日期 
			c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY); 
			break;
		case "5":
			// 返回星期五所在的日期 
			c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
			break;
		case "6":
			// 返回星期六所在的日期 
			c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
			break;
		case "0":
			// 返回星期日所在的日期 
			c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
			break;
		default:
			break;
		}
	    return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());  
	}  
	  
	/** 
	 * 根据一个日期，返回是星期几的字符串 
	 *  
	 * @param sdate 
	 * @return 
	 */  
	public static String getWeek(String sdate) {  
	    // 再转换为时间  
	    Date date = strToDate(sdate);  
	    Calendar c = Calendar.getInstance();  
	    c.setTime(date);  
	    // int hour=c.get(Calendar.DAY_OF_WEEK);  
	    // hour中存的就是星期几了，其范围 1~7  
	    // 1=星期日 7=星期六，其他类推  
	    return new SimpleDateFormat("EEEE").format(c.getTime());  
	}  
	  
	public static String getWeekStr(String sdate) {  
	    String str = "";  
	    str = getWeek(sdate);
	    switch (str) {
		case "1":
			str = "星期日"; 
			break;
		case "2":
			str = "星期一"; 
			break;
		case "3":
			str = "星期二"; 
			break;
		case "4":
			str = "星期三"; 
			break;
		case "5":
			str = "星期四";
			break;
		case "6":
			str = "星期五";
			break;
		case "7":
			str = "星期六"; 
			break;
		default:
			break;
		}
	    return str;  
	}  
	  
	/** 
	 * 两个时间之间的天数 
	 *  
	 * @param date1 
	 * @param date2 
	 * @return 
	 * @throws ParseException 
	 */  
	public static long getDays(String date1, String date2) throws ParseException {  
	    if (date1 == null || "".equals(date1)) {
	    	return 0;  
	    } 
	    if (date2 == null || "".equals(date2)) {
	    	return 0;  
	    } 
	    // 转换为标准时间  
	    SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");  
	    java.util.Date date = myFormatter.parse(date1);  
	    java.util.Date mydate = myFormatter.parse(date2);  
	    long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);  
	    return day;  
	}  
	  
	/** 
	 * 形成如下的日历 ， 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间 
	 * 此函数返回该日历第一行星期日所在的日期 
	 *  
	 * @param sdate 
	 * @return 
	 */  
	public static String getNowMonth(String sdate) {  
	    // 取该时间所在月的一号  
	    sdate = sdate.substring(0, 8) + "01";  
	    // 得到这个月的1号是星期几  
	    Date date = strToDate(sdate);  
	    Calendar c = Calendar.getInstance();  
	    c.setTime(date);  
	    int u = c.get(Calendar.DAY_OF_WEEK);  
	    String newday = getNextDay(sdate, (1 - u) + "");  
	    return newday;  
	}  
	  
	/** 
	 * 取得数据库主键 生成格式为yyyymmddhhmmss+k位随机数 
	 *  
	 * @param k 
	 *            表示是取几位随机数，可以自己定 
	 */  
	public static String getNo(int k) {  
	    return getUserDate("yyyyMMddhhmmss") + getRandom(k);  
	}  
	  
	/** 
	 * 返回一个随机数 
	 *  
	 * @param i 
	 * @return 
	 */  
	public static String getRandom(int i) {  
	    Random jjj = new Random();  
	    // int suiJiShu = jjj.nextInt(9);  
	    if (i == 0) {
	    	return "";  
	    } 
	    String jj = "";  
	    for (int k = 0; k < i; k++) {  
	        jj = jj + jjj.nextInt(9);  
	    }  
	    return jj;  
	}  
	  
	/** 
	 * @param
	 */  
	public static boolean RightDate(String date) {  
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
	    if (date == null) {
	    	return false;  
	    } 
	    if (date.length() > 10) {  
	        sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
	    } else {  
	        sdf = new SimpleDateFormat("yyyy-MM-dd");  
	    }  
	    try {  
	        sdf.parse(date);  
	    } catch (ParseException pe) {  
	        return false;  
	    }  
	    return true;  
	}  
	  
	/*************************************************************************** 
	 * //nd=1表示返回的值中包含年度 //yf=1表示返回的值中包含月份 //rq=1表示返回的值中包含日期 //format表示返回的格式 1 
	 * 以年月日中文返回 2 以横线-返回 // 3 以斜线/返回 4 以缩写不带其它符号形式返回 // 5 以点号.返回 
	 **************************************************************************/  
	public static String getStringDateMonth(String sdate, String nd, String yf,  
	        String rq, String format) {  
	    Date currentTime = new Date();  
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
	    String dateString = formatter.format(currentTime);  
	    String s_nd = dateString.substring(0, 4); // 年份  
	    String s_yf = dateString.substring(5, 7); // 月份  
	    String s_rq = dateString.substring(8, 10); // 日期  
	    String sreturn = "";  
	    if (sdate == null || "".equals(sdate) /*|| !Isdate(sdate)*/) { // 处理空值情况
	        //if (nd.equals("1")) {  
	    	if ("1".equals(nd)) {  
	            sreturn = s_nd;  
	            // 处理间隔符  
	            if ("1".equals(format)){  
	                sreturn = sreturn + "年";  
	            } else if ("2".equals(format)){   
	                sreturn = sreturn + "-";  
	            }  else if ("3".equals(format)){   
	                sreturn = sreturn + "/";  
	            } else if ("5".equals(format)) {  
	                sreturn = sreturn + "."; 
	            }     
	        }  
	        // 处理月份  
	        if ("1".equals(yf)) {  
	            sreturn = sreturn + s_yf;  
	            if ("1".equals(format)){  
	                sreturn = sreturn + "月";  
	            } else if ("2".equals(format)){  
	                sreturn = sreturn + "-";  
	            } else if ("3".equals(format)) { 
	                sreturn = sreturn + "/";  
	            } else if ("5".equals(format)) { 
	                sreturn = sreturn + "."; 
	            }     
	        }  
	        // 处理日期  
	        if ("1".equals(rq)) {  
	            sreturn = sreturn + s_rq;  
	            if ("1".equals(format)){
	            	sreturn = sreturn + "日";  
	            }  
	        }  
	    } else {  
	        // 不是空值，也是一个合法的日期值，则先将其转换为标准的时间格式  
	        // sdate = roc.util.RocDate.getOKDate(sdate);  
	        s_nd = sdate.substring(0, 4); // 年份  
	        s_yf = sdate.substring(5, 7); // 月份  
	        s_rq = sdate.substring(8, 10); // 日期  
	        if ("1".equals(nd)) {  
	            sreturn = s_nd;  
	            // 处理间隔符  
	            if ("1".equals(format)){  
	                sreturn = sreturn + "年";  
	            } else if ("2".equals(format)){   
	                sreturn = sreturn + "-";  
	            } else if ("3".equals(format)){   
	                sreturn = sreturn + "/";  
	            } else if ("5".equals(format)){   
	                sreturn = sreturn + ".";  
	            }     
	        }  
	        // 处理月份  
	        if ("1".equals(yf)) {  
	            sreturn = sreturn + s_yf;  
	            if ("1".equals(format)){  
	                sreturn = sreturn + "月";  
	            }else if ("2".equals(format)){  
	                sreturn = sreturn + "-";  
	            }else if ("3".equals(format)){  
	                sreturn = sreturn + "/";  
	            }else if ("5".equals(format)){  
	                sreturn = sreturn + ".";
	            }    
	        }  
	        // 处理日期  
	        if ("1".equals(rq)) {  
	            sreturn = sreturn + s_rq;  
	            if ("1".equals(format)){
	            	sreturn = sreturn + "日";  
	            }  
	        }  
	    }  
	    return sreturn;  
	}  
	  
	public static String getNextMonthDay(String sdate, int m) {  
	    sdate = getOKDate(sdate);  
	    int year = Integer.parseInt(sdate.substring(0, 4));  
	    int month = Integer.parseInt(sdate.substring(5, 7));  
	    month = month + m;  
	    if (month < 0) {  
	        month = month + 12;  
	        year = year - 1;  
	    } else if (month > 12) {  
	        month = month - 12;  
	        year = year + 1;  
	    }  
	    String smonth = "";  
	    if (month < 10) {
	    	smonth = "0" + month;  
	    } else{
	    	smonth = "" + month;  
	    }  
	    return year + "-" + smonth + "-10";  
	}  
	  
	public static String getOKDate(String sdate) {  
	    if (sdate == null || "".equals(sdate)){
	    	return getStringDateShort();  
	    }  
	    /*if (!Isdate(sdate)) {  
	        sdate = getStringDateShort();  
	    }*/  
	    // 将“/”转换为“-”  
	    sdate = sdate.replace("/", "-");  
	    // 如果只有8位长度，则要进行转换  
	    if (sdate.length() == 8) {
	    	sdate = sdate.substring(0, 4) + "-" + sdate.substring(4, 6) + "-"  
	    			+ sdate.substring(6, 8);  
	    } 
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
	    ParsePosition pos = new ParsePosition(0);  
	    Date strtodate = formatter.parse(sdate, pos);  
	    String dateString = formatter.format(strtodate);  
	    return dateString;  
	}  
	
	//获取两个时间之间的时间差（小时）
	public static long getDatePoor(Date endDate, Date nowDate) {
		 
	    long nd = 1000 * 24 * 60 * 60;
	    long nh = 1000 * 60 * 60;
	    long nm = 1000 * 60;
	    // long ns = 1000;
	    // 获得两个时间的毫秒时间差异
	    long diff = endDate.getTime() - nowDate.getTime();
	    // 计算差多少天
	    long day = diff / nd;
	    // 计算差多少小时
	    long hour = diff / nh;
	    // 计算差多少分钟
	    long min = diff % nd % nh / nm;
	    // 计算差多少秒//输出结果
	    // long sec = diff % nd % nh % nm / ns;
	    return hour;
	}

	//获取两个时间之间的时间差（天）
	public static long getDatePoorDay(Date endDate, Date nowDate) {

		long nd = 1000 * 24 * 60 * 60;
		long nh = 1000 * 60 * 60;
		long nm = 1000 * 60;
		// long ns = 1000;
		// 获得两个时间的毫秒时间差异
		long diff = endDate.getTime() - nowDate.getTime();
		// 计算差多少天
		long day = diff / nd;
		// 计算差多少小时
		long hour = diff / nh;
		// 计算差多少分钟
		long min = diff % nd % nh / nm;
		// 计算差多少秒//输出结果
		// long sec = diff % nd % nh % nm / ns;
		return day;
	}

	/**
	 * 获取当前年月日(格式:yyyy年MM月dd日)
	 * @return
	 */
	public static String getNowDateChina(){
		Date d = new Date();
		DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
		String s = df.format(d);
		
		return  s;
	}
	
	/**
	 * 比较两个日期的先后顺序
	 * @param date1(前面的日期)
	 * @param date2(后面的日期)
	 * @return error:比较错误,1:date2比date1晚.0:date2与date1日期相等.-1:date2比date1早
	 */
	public static String thenTwoDate(Date date1,Date date2){
		String then="error";
		if (date2.getTime()-date1.getTime() >0) {
			then = "1";
		}else if (date2.getTime()-date1.getTime() ==0) {
			then = "0";
		} else {
			then = "-1";
		}
		
		return then;
	}

	/**
	 * 返回前一天日期
	 * @param
	 * @param
	 * @return
	 */
	public static Date beforeDate(){
		Date date=new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		date = calendar.getTime();
		return date;
	}

	/**
	 * 获取当前月份
	 * @return
	 */
	public static int getMonth(){
		Date date=new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int i=calendar.get(Calendar.MONTH)+1;
		return i;
	}

	/**
	 * 返回前一天日期YYYY-MM-dd
	 * @param
	 * @param
	 * @return
	 */
	public static Date beforeDateType(){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date date=new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		date = calendar.getTime();
		String format = sdf.format(date);
		Date date1 = DateTimeUtil.strToDate(format);

		return date1;
	}

	/**
	 * 获取本周所有日期
	 * @return
	 */
	public static String nowWeekAll(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();

		// 今天是一周中的第几天
		int dayOfWeek = c.get(Calendar.DAY_OF_WEEK );

		if (c.getFirstDayOfWeek() == Calendar.SUNDAY) {
			c.add(Calendar.DAY_OF_MONTH, 1);
		}
		// 计算一周开始的日期
		c.add(Calendar.DAY_OF_MONTH, -dayOfWeek);

		StringBuffer stringBuffer = new StringBuffer();
		for (int i=1;i<=7;i++) {
			c.add(Calendar.DAY_OF_MONTH, 1);
			String format = sdf.format(c.getTime());
			stringBuffer.append("'"+format+"'");
			if(i!=7){
				stringBuffer.append(",");
			}
		}
		return stringBuffer.toString();
	}
    /**
     * 字符串格式转Date
     * @param time
     * @param format
     * @return
     */
	public static Date strToDate(String time,String format){
        Date date=null;
        DateFormat format1 = new SimpleDateFormat(format);
        try {
            date = format1.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }


	/**
	 * 获取本月所有日期
	 * @return
	 */
	public static String getAllTheDateOftheMonth() {
		StringBuffer stringBuffer = new StringBuffer();
		Date now = DateTimeUtil.getNow();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		cal.set(Calendar.DATE, 1);
		int month = cal.get(Calendar.MONTH);
		while(cal.get(Calendar.MONTH) == month){
			String format = sdf.format(cal.getTime());
			cal.add(Calendar.DATE, 1);
			stringBuffer.append("'"+format+"'");
			stringBuffer.append(",");
		}
		String substring = stringBuffer.toString().substring(0, stringBuffer.toString().length() - 1);
		return substring;
	}

	/**
	 * 获取两个日期之间所有日期的String类型
	 * @return
	 */
	public static String getBetweenDatesString(String beginStr, String endStr) {
		Date begin = DateTimeUtil.strToDate(beginStr);
		Date end = DateTimeUtil.strToDate(endStr);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		List<Date> betweenDates = DateTimeUtil.getBetweenDates(begin, end);

		StringBuffer stringBuffer = new StringBuffer();
		int i = 0;
		for (Date betweenDate : betweenDates) {
			i++;
			String format = sdf.format(betweenDate);
			stringBuffer.append("'"+format+"'");
			if(betweenDates.size()!=i){
				stringBuffer.append(",");
			}

		}
		return stringBuffer.toString();
	}

	/**
	 * 返回昨天 MM月dd日
	 * @return
	 */
	public static String returnMMdd(){
		Date date = DateTimeUtil.beforeDateType();
		DateFormat  df  = new SimpleDateFormat("MM月dd日");
		return df.format(date);
	}

	/**
	 * 返回前一天日期YYYY-MM-dd
	 * @return
	 */
	public static String beforeStringDate(){
		Date date = DateTimeUtil.beforeDateType();
		String s = DateTimeUtil.dateToStr(date);
		return s;

	}

	/**
	 * 获取当前日期前一天的本周所有日期
	 * @return
	 */
	public static String beforeWeekAll(){
		Calendar cal = Calendar.getInstance();
		Date date = DateTimeUtil.beforeDateType();
		cal.setTime(date);

		int d = 0;
		if(cal.get(Calendar.DAY_OF_WEEK)==1){
			d = -6;
		}else{
			d = 2-cal.get(Calendar.DAY_OF_WEEK);
		}
		cal.add(Calendar.DAY_OF_WEEK, d);
		//所在周开始日期
		String format1 = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
		cal.add(Calendar.DAY_OF_WEEK, 6);
		//所在周结束日期
		String format2 = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
		String betweenDatesString = DateTimeUtil.getBetweenDatesString(format1, format2);

		return betweenDatesString;
	}

	/**
	 * 获取当前日期前一天的本月所有日期
	 * @return
	 */
	public static String beforeMonthAll(){
		Date date = DateTimeUtil.beforeDateType();
		 Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int first = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
		cal.set(Calendar.DAY_OF_MONTH, first);
		Date day_first = cal.getTime();
		String str_day_first = DateTimeUtil.dateToStr(day_first);

		int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		cal.set(Calendar.DAY_OF_MONTH, last);
		Date day_last = cal.getTime();
		String str_day_last = DateTimeUtil.dateToStr(day_last);

		String betweenDatesString = DateTimeUtil.getBetweenDatesString(str_day_first, str_day_last);

		return betweenDatesString;
	}

	/**
	 * 获得当天0点时间
	 * @return
	 */
	public static long getTimesmorning(){
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return (Long) (cal.getTimeInMillis());
	}

	/**
	 * 获得当天24点时间
	 * @return
	 */
	public static long getTimesnight(){
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 24);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return (Long) (cal.getTimeInMillis());
	}

	/**
	 * 获得某天24点时间,返回Date
	 * @return
	 */
	public static Date getTime24(Date parameterDate) throws ParseException {
		Calendar cal = Calendar.getInstance();
		cal.setTime(parameterDate);
		cal.set(Calendar.HOUR_OF_DAY, 24);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		long timeInMillis = cal.getTimeInMillis();
		Date date = ssTimeTogetYear(timeInMillis);
		return  date;
	}
	/**
	 * 获得某天0点时间,返回Date
	 * @return
	 */
	public static Date getTime00(Date parameterDate) throws ParseException {
		Calendar cal = Calendar.getInstance();
		cal.setTime(parameterDate);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		Long timeInMillis = (cal.getTimeInMillis());
		Date date = ssTimeTogetYear(timeInMillis);
		return date;
	}

	/**
	 * 获得当天24点时间,返回Date
	 * @return
	 */
	public static Date getTime24() throws ParseException {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 24);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		long timeInMillis = cal.getTimeInMillis();
		Date date = ssTimeTogetYear(timeInMillis);
		return  date;
	}
	/**
	 * 获得当天0点时间,返回Date
	 * @return
	 */
	public static Date getTime00() throws ParseException {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		Long timeInMillis = (cal.getTimeInMillis());
		Date date = ssTimeTogetYear(timeInMillis);
		return date;
	}


	/**
	 * 获得指定日期的前一天
	 * @param specifiedDay
	 * @return
	 * @throws Exception
	 */
	public static String getSpecifiedDayBefore(String specifiedDay){
//SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		Date date=null;
		try {
			date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day=c.get(Calendar.DATE);
		c.set(Calendar.DATE,day-1);

		String dayBefore=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
		return dayBefore;
	}

	/**
	 * 获取指定日期后一天的日期
	 * @param specifiedDay
	 * @return
	 */
	public static String getSpecifiedDayAfter(String specifiedDay){
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day1 = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day1 + 1);

		String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());

		return dayAfter;
	}

	/**
	 * 将短时间格式字符串转换为时间 yyyy-MM-dd 00:00:00
	 *
	 * @param strDate00
	 * @return
	 */
	public static Date strToDate00(String strDate00) {
		String s = strDate00.substring(0,10) + " 00:00:00";
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(s, pos);
		return strtodate;
	}

	/**
	 * 将短时间格式字符串转换为时间 yyyy-MM-dd 23:59:59
	 *
	 * @param strDate
	 * @return
	 */
	public static Date strToDate24(String strDate) {
		String s = strDate.substring(0,10) + " 23:59:59";
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(s, pos);
		return strtodate;
	}

	public  static Long newDateToLong(){
		Date date = new Date();
		long time = date.getTime();
		return time;
	}

	/**
	 * 测试字符串是否是数字
	 * @param string
	 * @return
	 */
	public static boolean isNumber(String string) {
		if (string == null){
			return false;
		}else {
			Pattern pattern = Pattern.compile("^-?\\d+(\\.\\d+)?$");
			return pattern.matcher(string).matches();
		}

	}

	public static void main(String[] args) throws ParseException {
		System.out.println(isNumber("123"));
		System.out.println(isNumber("-123.456"));
		System.out.println(isNumber("123成宇佳456"));
	}

}
